package com.wanmait.wisdom.config;

import com.wanmait.wisdom.interceptor.JWTInterceptor;
import com.wanmait.wisdom.interceptor.web.WebJWTInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebConfig implements WebMvcConfigurer {
/*
    @Resource
    private InitInterceptor initInterceptor;*/

    @Resource
    private WebJWTInterceptor webJWTInterceptor;
    @Resource
    private JWTInterceptor jwtInterceptor;

    //统计对跨域资源共享进行配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //.allowedOrigins("http://www.wanmait.com","http://www.qingruanit.net")
                .allowedOrigins("*")
                //.allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(60*60*2);//单位秒，2小时内对于复杂的请求不用重新发送预检请求

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(initInterceptor).addPathPatterns("/**").excludePathPatterns("/manage/login","/manage/index");
        registry.addInterceptor(webJWTInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login","/manage/index")
        .addPathPatterns("/web/**").excludePathPatterns("/web/login","/web/uploadCardPicFacade","/web/sendCode","/web/loginWithPhone",
                "/web/uploadCardPicBack",
                "/web/uploadPic");
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/manage/**").excludePathPatterns("/manage/login","/manage/index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:static/");

    }
}
